//
//  ZYNViewControllerForAllSongList.m
//  VideoPic
//
//  Created by dllo on 16/3/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYNViewControllerForAllSongList.h"
#import "ZYNCollectionViewCellForAllSongList.h"
#import "NetworkingHandle.h"
#import "ZYNViewControllerForHotSongListDetail.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ZYNViewControllerForAllSongList ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *arrForAllSongList;
@property (nonatomic, strong) NSMutableArray *arrForHeader;
@property (nonatomic, strong) NSMutableDictionary *dicForAllSongList;
@property (nonatomic, strong) NSString *strForChangjing;
@property (nonatomic, strong) NSString *hang;
@property (nonatomic, strong) NSString *shu;
@end

@implementation ZYNViewControllerForAllSongList

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.arrForAllSongList = [NSMutableArray array];
    self.arrForHeader = [NSMutableArray array];
    [self handleData];
    [self createCollection];
}
- (void)handleData {
    [NetworkingHandle handleWorkData:@"http://api.dongting.com/song/tag/songlist" AndBody:nil value:nil handleBlock:^(id result) {
    
        for (NSDictionary *dictionary in result[@"data"]) {
            
            self.strForChangjing = dictionary[@"tag"];
            
            [self.arrForHeader addObject:self.strForChangjing];
            
            NSLog(@"1598%@",self.arrForHeader);
            
            NSMutableArray *temp = [NSMutableArray array];
            
            for (NSDictionary *dic in dictionary[@"children"]) {
                
                self.strForChangjing = dic[@"tag"];
                
                [temp addObject:self.strForChangjing];
                
            }
            
            [self.arrForAllSongList addObject:temp];
            NSLog(@"1246:%@", self.arrForAllSongList);
            
        }
        [self.collection reloadData];
    }];

}
- (void)createFlowLayout {
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((WIDTH - 25) / 4, WIDTH / 8);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 5;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
}
- (void)createCollection {
    [self createFlowLayout];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) collectionViewLayout:self.flowLayout];
    
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    [self.view addSubview:self.collection];
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.collection registerClass:[ZYNCollectionViewCellForAllSongList class] forCellWithReuseIdentifier:@"pool1"];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return self.arrForAllSongList.count;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [[self.arrForAllSongList objectAtIndex:section] count];
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ZYNCollectionViewCellForAllSongList *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0.835 green:0.835 blue:0.835 alpha:1.00];
    cell.labelForType.text = self.arrForAllSongList[indexPath.section][indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ZYNViewControllerForHotSongListDetail *list = [[ZYNViewControllerForHotSongListDetail alloc] init];
    list.type = YES;
    list.strForChangjing = self.arrForAllSongList[indexPath.section][indexPath.row];
    NSLog(@"%@", list.strForChangjing);
    [self.navigationController pushViewController:list animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
